package com.jikexueyuan.drawingboard;

import android.content.Context;
import android.graphics.PointF;

import java.util.Timer;
import java.util.TimerTask;

import android.media.MediaPlayer;
import android.util.AttributeSet;
import com.jikexueyuan.drawingboard.PenTrackContainer;
import com.jikexueyuan.drawingboard.MyView;

/**
 * Created by Administrator on 2015/3/20.
 */
public class PenTrackReplay {
    private final int BUFFSIZE = 1000;
    public long mBaseTimeStamp;
    public PenTrackContainer[] mPenTrack = new PenTrackContainer[BUFFSIZE];
    private int mTrackCount = 0;
    private long mTimeGapNext = 0;
    private MyView mDrawView;
    private boolean mReplayEnable = false;

    public PenTrackReplay(MyView view) {
        mDrawView = view;
        mBaseTimeStamp = 0;
        System.out.println("PenTrackReplay(): ");

        // 可以用双buffer交替存文件，这样就不用担心内存占用了，也不用搞队列了。
        for (int i = 0; i < BUFFSIZE; i++) {
            mPenTrack[i] = new PenTrackContainer();
        }
    }

    public void record(PenTrackContainer penTrack, int count_point) {
        mPenTrack[count_point].setPenTrackPoint(penTrack);
    }

    public void replay(VoiceRecorder voiceRecord) {
        long timeGap;
        MediaPlayer player = new MediaPlayer();
        String voiceRecordPath = null;

        mReplayEnable = true;
        mTrackCount = 0;
        mBaseTimeStamp = mPenTrack[0].getTimeStamp();
        System.out.println("PenTrackReplay.replay(): mBaseTimeStamp=" + mBaseTimeStamp);
        for (int i = 0; i < BUFFSIZE; i++) {
            if(mPenTrack[i].getTimeStamp() != 0) {
                timeGap = mPenTrack[i+1].getTimeStamp() - mPenTrack[i].getTimeStamp();
                if(timeGap > 0)
                    mPenTrack[i].setTimeGapNext(timeGap);

                System.out.println("PenTrackReplay.replay(): i=" + i + ", time=" + (mPenTrack[i].getTimeStamp()-mBaseTimeStamp)
                        + ", timeGapNext=" + mPenTrack[i].getTimeGapNext());
            }
        }

        voiceRecordPath = voiceRecord.getVoiceRecordPath();
        if(voiceRecordPath != null) {
            try {
                player.setDataSource(voiceRecordPath);
                player.prepare();
                player.start();
            } catch (Exception e) {
                System.out.println("PenTrackReplay.replay(): player");
            }
        }
        mDrawView.clear();
        PenTrackTimer1();
    }

    private void PenTrackTimer1() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                mTimeGapNext = mPenTrack[mTrackCount].getTimeGapNext();
                mDrawView.redrawPenTrack(mPenTrack[mTrackCount].getAction() ,mPenTrack[mTrackCount].getPoint());

                System.out.println("PenTrackTimer1() mTrackCount=" + mTrackCount + ", mTimeGapNext=" + mTimeGapNext
                        + ", current_time=" + System.currentTimeMillis());
                mTrackCount++;
                if(mTimeGapNext <= 0){
                    mReplayEnable = false;
                    return ;
                }
                PenTrackTimer2();
            }
        }, mTimeGapNext);// 设定指定的时间time,此处为距离下个点的间隔时间(ms)
    }

    private void PenTrackTimer2() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                mTimeGapNext = mPenTrack[mTrackCount].getTimeGapNext();
                mDrawView.redrawPenTrack(mPenTrack[mTrackCount].getAction() ,mPenTrack[mTrackCount].getPoint());

                System.out.println("PenTrackTimer2() mTrackCount=" + mTrackCount + ", mTimeGapNext=" + mTimeGapNext
                        + ", current_time=" + System.currentTimeMillis());
                mTrackCount++;
                if(mTimeGapNext <= 0){
                    mReplayEnable = false;
                    return ;
                }
                PenTrackTimer1();
            }
        }, mTimeGapNext);// 设定指定的时间time,此处为距离下个点的间隔时间(ms)
    }

    public void clearBuffer() {
        for (int i = 0; i < BUFFSIZE; i++) {
            mPenTrack[i].clearPoint();
        }
    }

    public boolean getReplayEnable() {
        return mReplayEnable;
    }
}
